package com.lanlian.cayman.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.InformationDetailActivity;
import com.lanlian.cayman.adapter.LionAnalysisAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseLazyFragment;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.InformationInfo;
import com.lanlian.commonlib.bean.InformationListInfo;
import com.lanlian.commonlib.bean.ReqInformationList;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.OnEventID;
import com.lanlian.commonlib.util.OnEventUtils;
import com.lanlian.uikit.RecyclerViewDivider;
import com.lanlian.utillib.CommonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : xiaobo.hu@foxtry.com
 *     date   : 2018/8/9 13:40
 *     desc   : 资讯TAB
 *     version: 1.0
 * </pre>
 */
@SuppressLint("ValidFragment")
public class LionAnalysisFragment extends BaseLazyFragment {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;

    private ArrayList<InformationInfo> mList = new ArrayList<>();
    private LionAnalysisAdapter mAdapter;
    private int mPage = 1;

    private OnRefreshBanner mOnRefreshBanner;

    public interface OnRefreshBanner {
        void onRefresh();
    }

    public LionAnalysisFragment() {
    }

    public LionAnalysisFragment(OnRefreshBanner onRefreshBanner) {
        this.mOnRefreshBanner = onRefreshBanner;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lion_analysis, null);
        initView(view);
        setListeners();
        initAdapter();
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mLayRefresh = view.findViewById(R.id.lay_refresh);
        mRecycleView = view.findViewById(R.id.recycle_view);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mOnRefreshBanner.onRefresh();
                mPage = 1;
                getInformation();
            }
        });

        mLayRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPage++;
                getInformation();
            }
        });
    }

    @Override
    protected void loadData() {
        mLayRefresh.autoRefresh(500);
    }

    private void initAdapter() {
        mAdapter = new LionAnalysisAdapter(R.layout.adapter_lion_analysis, mList);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecycleView.addItemDecoration(new RecyclerViewDivider(getActivity(), LinearLayoutManager.VERTICAL));
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                OnEventUtils.onEvent(getActivity(), OnEventID.INFORMATION, mList.get(position).getTitle());
                startActivity(new Intent(getActivity(), InformationDetailActivity.class)
                        .putExtra("id", mList.get(position).getId()));
            }
        });
    }

    private void getInformation() {
        ReqInformationList reqInformationList = new ReqInformationList();
        reqInformationList.setType(466);
        reqInformationList.setPage(mPage);
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), reqInformationList, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                if (mPage == 1) {
                    mList.clear();
                    mLayRefresh.finishRefresh();
                } else {
                    mLayRefresh.finishLoadMore();
                }
                String string = (String) response;
                string = string.substring(1, string.length() - 1);
                string = string.substring(string.indexOf("]") + 2, string.length());
                InformationListInfo listInfo = new InformationListInfo();
                try {
                    listInfo = new Gson().fromJson(string, InformationListInfo.class);
                } catch (Exception e) {
                    e.fillInStackTrace();
                }
                mList.addAll(listInfo.getData());
                mAdapter.setNewData(mList);
                if (CommonUtils.isListEmpty(listInfo.getData())) {
                    mLayRefresh.setNoMoreData(true);
                } else {
                    mLayRefresh.setNoMoreData(false);
                }
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mLayRefresh.finishRefresh();
                mLayRefresh.finishLoadMore();
            }
        });
    }

}
